package com.fitness.front.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fitness.front.convertor.CourseReserveConvertor;
import com.fitness.front.modal.course.dto.CourseReserveDto;
import com.fitness.front.modal.course.entity.Course;
import com.fitness.front.modal.course.entity.CourseReserve;
import com.fitness.front.modal.course.vo.CourseReserveVO;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.ICourseReserveService;
import com.fitness.front.service.ICourseService;
import com.fitness.front.service.IMemberCourseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.flume.EventDeliveryException;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/front/courseReserve")
public class CourseReserveController {
    private final CourseReserveConvertor courseReserveConvertor;
    private final ICourseReserveService courseReserveService;
    private final ICourseService courseService;
    private final IMemberCourseService memberCourseService;

    /**
     * 预约课程
     */
    @PostMapping("/reserveCourse")
    public ServerResponseEntity<CourseReserveVO> reserveCourse(@RequestBody CourseReserveDto reserveDto) throws EventDeliveryException {
        Long courseId = reserveDto.getCourseId();
        Long memberId = reserveDto.getMemberId();
        QueryWrapper<CourseReserve> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.eq("member_id", memberId);
        queryWrapper.and(qw->qw.eq("status", ICourseReserveService.COURSE_RESERVE_NO_PAY)
                .or().eq("status", ICourseReserveService.COURSE_RESERVE_PAY_FAILURE)
                .or().eq("status", ICourseReserveService.COURSE_RESERVE_PAY_SUCCESS));
        CourseReserve courseReserve = courseReserveService.getOne(queryWrapper);
        if(courseReserve != null){
            return ServerResponseEntity.fail("RC00001","已经预约过该课程");
        }

        Course course = courseService.getById(courseId);
        Integer personCnt = course.getPersonCnt();
        Integer reserveCnt = course.getReserveCnt();
        if(Objects.equals(personCnt, reserveCnt)){
            return ServerResponseEntity.fail("RC00001","预约人数已满");
        }
        CourseReserve newCourseReserve = courseReserveConvertor.dto2Entity(reserveDto);
        courseReserveService.reserveCourse(newCourseReserve);
        return ServerResponseEntity.success(courseReserveConvertor.entity2Vo(newCourseReserve));
    }

    /**
     * 支付
     */
    @RequestMapping("/payment")
    public ServerResponseEntity<Boolean> payment(@RequestParam("memberId") Long memberId,
                                                 @RequestParam("memberName") String memberName,
                                                 @RequestParam("courseId") Long courseId,
                                                 @RequestParam("amount") Long amount,
                                                 @RequestParam("remark")String remark) {
        boolean result = courseReserveService.payment(courseId,memberId,memberName,amount,remark);
        return ServerResponseEntity.success(result);
    }
}
